<template>
<div class="app-container">
<!-- 供应商查询条件 -->
  <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
    <el-form-item label="供应商名称" prop="supplierName">
      <el-input
        v-model="queryParams.supplierName"
        placeholder="请输入供应商名称"
        clearable
        @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="状态" prop="isActive">
      <el-input
        v-model="queryParams.isActive"
        placeholder="请输入状态"
        clearable
        @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="创建时间">
      <el-date-picker
        v-model="daterangeCreatedAt"
        style="width: 240px"
        value-format="yyyy-MM-dd"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
    </el-form-item>
  </el-form>
  <!-- 供应商列表 -->
  <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column label="供应商ID" align="center" prop="supplierId" />
    <el-table-column label="供应商名称" align="center" prop="supplierName" />
    <el-table-column label="供应商联系人" align="center" prop="contactPerson" />
    <el-table-column label="供应商联系电话" align="center" prop="contactPhone" />
    <el-table-column label="创建时间" align="center" prop="createdAt" width="180" />
    <el-table-column label="启用状态" align="center" prop="isActive">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.erp_supplier_isactive" :value="scope.row.isActive" />
      </template>
    </el-table-column>
    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
      <template slot-scope="scope">
        <el-button
          size="mini"
          type="text"
          icon="el-icon-edit"
          @click="handleUpdate(scope.row)">
        >修改</el-button>

        <el-button
          size="mini"
          type="text"
          :icon="scope.row.isActive == 1 ? 'el-icon-close' : 'el-icon-check'"
          @click="handleToggleStatus(scope.row)">
          {{ scope.row.isActive == 1 ? '禁用' : '启用' }}
        </el-button>
      </template>
    </el-table-column>

  </el-table>
  <!-- 添加或修改供应商信息对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
      </el-form-item>
      <el-form-item label="供应商联系人" prop="contactPerson">
        <el-input v-model="form.contactPerson" placeholder="请输入供应商联系人" />
      </el-form-item>
      <el-form-item label="供应商联系电话" prop="contactPhone">
        <el-input v-model="form.contactPhone" placeholder="请输入供应商联系电话" />
      </el-form-item>
      <el-form-item label="是否启用" prop="isActive">
        <el-radio-group v-model="form.isActive">
          <el-radio label="1">有效</el-radio>
          <el-radio label="0">终止合作</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
  <pagination
    v-show="total>0"
    :total="total"
    :page.sync="queryParams.pageNum"
    :limit.sync="queryParams.pageSize"
    @pagination="getList"
  />
</div>
</template>

<script>
import {addSupplier, getSupplierList, updateSupplier} from "@/api/erp/supplier";
import dict from "@/utils/dict";
import {getToken} from "@/utils/auth";

export default {
  name: "Supplier",
  dicts:['erp_supplier_isactive'],
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/erp/supplier/importData"
      },
      //时间范围
      daterangeCreatedAt: [],
      // 显示搜索条件
      showSearch: true,
      supplierList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      supplierId: null,
      total:0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierName: null,
        contactPerson: null,
        contactPhone: null,
        createdAt: null,
        isActive: null,
        beginCreatedAt: null,
        endCreatedAt: null,
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        supplierName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        contactPerson: [
          { required: true, message: "供应商联系人不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "供应商联系电话不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 切换供应商状态
    handleToggleStatus(row) {
      const action = row.isActive == 1 ? '禁用' : '启用';
      const newStatus = row.isActive == 1 ? 0 : 1;

      this.$modal.confirm(`确定要${action}供应商 "${row.supplierName}" 吗?`).then(() => {
        return updateSupplier({ ...row, isActive: newStatus });
      }).then(() => {
        this.$modal.msgSuccess(`${action}成功`);
        this.getList();
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.supplierId != null) {
            updateSupplier(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSupplier(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 取消
    cancel() {
      this.open = false
      this.form={}
    },
    //新增
    handleAdd(){
      this.title="新增供应商"
      this.open=true
      this.submitForm()
    },
    handleUpdate(row){
      this.form=row
      this.title="修改供应商"
      this.open=true
    },
    getList(){
      this.queryParams.params = {}
      if (null != this.daterangeCreatedAt && '' != this.daterangeCreatedAt) {
        this.queryParams.beginCreatedAt= this.daterangeCreatedAt[0]
        this.queryParams.endCreatedAt= this.daterangeCreatedAt[1]
      }
      getSupplierList(this.queryParams).then(res=>{
        console.log(this.daterangeCreatedAt)
        console.log(this.queryParams.beginCreatedAt)
        console.log(this.queryParams.endCreatedAt)
        this.supplierList=res.rows
        this.total=res.total
        console.log(res)
        this.loading=false
      }).catch(()=>{
        this.loading=false
      })
    },
    //多选框选中数据
    handleSelectionChange(selection){
      this.ids = selection.map(item => item.supplierId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    resetQuery() {
      this.daterangeCreatedAt = []
      this.queryParams.beginCreatedAt = null
      this.queryParams.endCreatedAt = null
      this.queryParams.supplierName = null
      this.queryParams.isActive = null
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
  },
}
</script>

<style scoped lang="scss">

</style>
